/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZFilterRule;
/*     */ import com.zimbra.client.ZFilterRules;
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import java.util.List;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestFolderFilterRules
/*     */   extends TestCase
/*     */ {
/*  36 */   private static String USER_NAME = "user1";
/*     */   
/*  38 */   private static String NAME_PREFIX = "TestFolderFilterRules";
/*     */   
/*  40 */   private static String FOLDER1_NAME = NAME_PREFIX + "1";
/*  41 */   private static String FOLDER2_NAME = NAME_PREFIX + "2";
/*  42 */   private static String FOLDER3_NAME = NAME_PREFIX + "3";
/*  43 */   private static String FOLDER4_NAME = NAME_PREFIX + "4";
/*     */   
/*  45 */   private static String SUBJECT1 = NAME_PREFIX + " 1";
/*  46 */   private static String SUBJECT2 = NAME_PREFIX + " 2";
/*  47 */   private static String SUBJECT3 = NAME_PREFIX + " 3";
/*  48 */   private static String SUBJECT4 = NAME_PREFIX + " 4";
/*     */   
/*     */ 
/*     */   private ZFolder mFolder1;
/*     */   
/*     */ 
/*     */   private ZFolder mFolder2;
/*     */   
/*     */   private ZFolder mFolder3;
/*     */   
/*     */   private ZFolder mFolder4;
/*     */   
/*     */   private ZFilterRules mOriginalRules;
/*     */   
/*     */ 
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  66 */     super.setUp();
/*  67 */     cleanUp();
/*     */     
/*  69 */     ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
/*  70 */     this.mFolder1 = TestUtil.createFolder(mbox, FOLDER1_NAME);
/*  71 */     this.mFolder2 = TestUtil.createFolder(mbox, this.mFolder1.getId(), FOLDER2_NAME);
/*  72 */     this.mFolder3 = TestUtil.createFolder(mbox, this.mFolder2.getId(), FOLDER3_NAME);
/*  73 */     this.mFolder4 = TestUtil.createFolder(mbox, FOLDER4_NAME);
/*     */     
/*     */ 
/*  76 */     this.mOriginalRules = mbox.getIncomingFilterRules();
/*  77 */     TestUtil.setAccountAttr(USER_NAME, "zimbraMailSieveScript", FILTER_RULES);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testDefault()
/*     */     throws Exception
/*     */   {
/*  85 */     sendMessages();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testRenameLeaf()
/*     */     throws Exception
/*     */   {
/*  93 */     renameFolder(this.mFolder3.getId(), NAME_PREFIX + "New3", null);
/*  94 */     sendMessages();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testRenameRoot()
/*     */     throws Exception
/*     */   {
/* 104 */     renameFolder(this.mFolder1.getId(), NAME_PREFIX + "New1", null);
/* 105 */     sendMessages();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testMoveLeaf()
/*     */     throws Exception
/*     */   {
/* 113 */     moveFolder(this.mFolder3.getId(), this.mFolder4.getId());
/* 114 */     sendMessages();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testMoveParent()
/*     */     throws Exception
/*     */   {
/* 122 */     moveFolder(this.mFolder2.getId(), this.mFolder4.getId());
/* 123 */     sendMessages();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testMoveAndRename()
/*     */     throws Exception
/*     */   {
/* 131 */     renameFolder(this.mFolder2.getId(), NAME_PREFIX + "New2", this.mFolder4.getId());
/* 132 */     sendMessages();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMoveAndChangeCase()
/*     */     throws Exception
/*     */   {
/* 141 */     String newName = this.mFolder2.getName().toUpperCase();
/* 142 */     renameFolder(this.mFolder2.getId(), newName, this.mFolder4.getId());
/* 143 */     sendMessages();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testDeleteFolder()
/*     */     throws Exception
/*     */   {
/* 152 */     ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
/* 153 */     mbox.deleteFolder(this.mFolder2.getId());
/*     */     
/*     */ 
/*     */ 
/* 157 */     TestUtil.addMessageLmtp(SUBJECT2, USER_NAME, USER_NAME);
/* 158 */     TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT2 + "\"");
/* 159 */     TestUtil.addMessageLmtp(SUBJECT3, USER_NAME, USER_NAME);
/* 160 */     TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT3 + "\"");
/*     */     
/*     */ 
/* 163 */     List<ZFilterRule> rules = mbox.getIncomingFilterRules(true).getRules();
/* 164 */     assertEquals(4, rules.size());
/* 165 */     for (ZFilterRule rule : rules) {
/* 166 */       if (rule.getName().equals("Folder 1")) {
/* 167 */         assertTrue(rule.isActive());
/* 168 */       } else if (rule.getName().equals("Folder 2")) {
/* 169 */         assertFalse(rule.isActive());
/* 170 */       } else if (rule.getName().equals("Folder 3")) {
/* 171 */         assertFalse(rule.isActive());
/* 172 */       } else if (rule.getName().equals("Folder 4")) {
/* 173 */         assertTrue(rule.isActive());
/*     */       } else {
/* 175 */         fail("Unexpected rule name: " + rule.getName());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMoveFolderToTrash()
/*     */     throws Exception
/*     */   {
/* 186 */     ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
/* 187 */     mbox.trashFolder(this.mFolder2.getId());
/*     */     
/*     */ 
/*     */ 
/* 191 */     TestUtil.addMessageLmtp(SUBJECT2, USER_NAME, USER_NAME);
/* 192 */     TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT2 + "\"");
/* 193 */     TestUtil.addMessageLmtp(SUBJECT3, USER_NAME, USER_NAME);
/* 194 */     TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT3 + "\"");
/*     */     
/*     */ 
/* 197 */     List<ZFilterRule> rules = mbox.getIncomingFilterRules(true).getRules();
/* 198 */     assertEquals(4, rules.size());
/* 199 */     for (ZFilterRule rule : rules) {
/* 200 */       if (rule.getName().equals("Folder 1")) {
/* 201 */         assertTrue(rule.isActive());
/* 202 */       } else if (rule.getName().equals("Folder 2")) {
/* 203 */         assertFalse(rule.isActive());
/* 204 */       } else if (rule.getName().equals("Folder 3")) {
/* 205 */         assertFalse(rule.isActive());
/* 206 */       } else if (rule.getName().equals("Folder 4")) {
/* 207 */         assertTrue(rule.isActive());
/*     */       } else {
/* 209 */         fail("Unexpected rule name: " + rule.getName());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void sendMessages()
/*     */     throws Exception
/*     */   {
/* 219 */     verifyFolderSize(this.mFolder1.getId(), 0);
/* 220 */     TestUtil.addMessageLmtp(SUBJECT1, USER_NAME, USER_NAME);
/* 221 */     verifyFolderSize(this.mFolder1.getId(), 1);
/*     */     
/* 223 */     verifyFolderSize(this.mFolder2.getId(), 0);
/* 224 */     TestUtil.addMessageLmtp(SUBJECT2, USER_NAME, USER_NAME);
/* 225 */     verifyFolderSize(this.mFolder2.getId(), 1);
/*     */     
/* 227 */     verifyFolderSize(this.mFolder3.getId(), 0);
/* 228 */     TestUtil.addMessageLmtp(SUBJECT3, USER_NAME, USER_NAME);
/* 229 */     verifyFolderSize(this.mFolder3.getId(), 1);
/*     */     
/* 231 */     verifyFolderSize(this.mFolder4.getId(), 0);
/* 232 */     TestUtil.addMessageLmtp(SUBJECT4, USER_NAME, USER_NAME);
/* 233 */     verifyFolderSize(this.mFolder4.getId(), 1);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void verifyFolderSize(String folderId, int size)
/*     */     throws Exception
/*     */   {
/* 241 */     ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
/* 242 */     ZFolder f = mbox.getFolderById(folderId);
/* 243 */     List<ZMessage> messages = TestUtil.search(mbox, "in:" + f.getPath());
/* 244 */     assertEquals("Incorrect message count for folder " + f.getPath(), size, messages.size());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private void renameFolder(String folderId, String newName, String newParentId)
/*     */     throws Exception
/*     */   {
/* 253 */     ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
/* 254 */     ZFolder folder = mbox.getFolderById(folderId);
/*     */     
/*     */ 
/* 257 */     String oldPath = folder.getPath();
/* 258 */     if (oldPath.charAt(0) == '/')
/*     */     {
/* 260 */       oldPath = oldPath.substring(1, oldPath.length());
/*     */     }
/* 262 */     Account account = TestUtil.getAccount(USER_NAME);
/* 263 */     String script = account.getAttr("zimbraMailSieveScript");
/* 264 */     assertTrue("Could not find path " + oldPath + " in script: " + script, script.contains(oldPath));
/*     */     
/*     */ 
/* 267 */     mbox.renameFolder(folderId, newName, newParentId);
/* 268 */     folder = mbox.getFolderById(folder.getId());
/* 269 */     String newPath = folder.getPath();
/* 270 */     if (newPath.charAt(0) == '/') {
/* 271 */       newPath = newPath.substring(1, newPath.length());
/*     */     }
/* 273 */     assertEquals(newName, folder.getName());
/* 274 */     assertTrue("Folder path '" + newPath + "' does not end with " + newName, folder.getPath().endsWith(newName));
/*     */     
/*     */ 
/*     */ 
/* 278 */     account = TestUtil.getAccount(USER_NAME);
/* 279 */     script = account.getAttr("zimbraMailSieveScript");
/* 280 */     assertFalse("Found old path '" + oldPath + " in script: " + script, script.indexOf(oldPath) >= 0);
/* 281 */     assertTrue("Could not find new path '" + newPath + " in script: " + script, script.indexOf(newPath) >= 0);
/*     */   }
/*     */   
/*     */   private void moveFolder(String folderId, String newParentFolderId) throws Exception
/*     */   {
/* 286 */     ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
/* 287 */     ZFolder folder = mbox.getFolderById(folderId);
/* 288 */     ZFolder newParent = mbox.getFolderById(newParentFolderId);
/* 289 */     String newParentPath = newParent.getPath();
/* 290 */     String oldPath = folder.getPath();
/* 291 */     if (oldPath.charAt(0) == '/')
/*     */     {
/* 293 */       oldPath = oldPath.substring(1, oldPath.length());
/*     */     }
/*     */     
/*     */ 
/* 297 */     Account account = TestUtil.getAccount(USER_NAME);
/* 298 */     String script = account.getAttr("zimbraMailSieveScript");
/* 299 */     assertTrue("Could not find path " + oldPath + " in script: " + script, script.contains(oldPath));
/*     */     
/* 301 */     mbox.moveFolder(folderId, newParentFolderId);
/* 302 */     folder = mbox.getFolderById(folder.getId());
/* 303 */     String newPath = folder.getPath();
/* 304 */     assertFalse("Path '" + oldPath + "' did not change", newPath.equals(oldPath));
/* 305 */     assertTrue("Folder path '" + newPath + "' does not start with " + newParentPath, folder.getPath().startsWith(newParentPath));
/*     */     
/*     */ 
/*     */ 
/* 309 */     account = TestUtil.getAccount(USER_NAME);
/* 310 */     script = account.getAttr("zimbraMailSieveScript");
/* 311 */     assertFalse("Found old path '" + oldPath + " in script: " + script, script.indexOf(oldPath) >= 0);
/* 312 */     assertTrue("Could not find new path '" + newPath + " in script: " + script, script.indexOf(newPath) >= 0);
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 317 */     TestUtil.deleteTestData(USER_NAME, NAME_PREFIX);
/* 318 */     TestUtil.deleteTestData(USER_NAME, NAME_PREFIX.toUpperCase());
/*     */   }
/*     */   
/*     */   protected void tearDown() throws Exception {
/* 322 */     ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
/* 323 */     mbox.saveIncomingFilterRules(this.mOriginalRules);
/* 324 */     cleanUp();
/*     */   }
/*     */   
/* 327 */   private static final String FILTER_RULES = StringUtil.join("\n", new String[] { "require [\"fileinto\", \"reject\", \"tag\", \"flag\"];", "", "# Folder 1", "if anyof (header :is \"subject\" \"" + SUBJECT1 + "\" )", "{", "    fileinto \"" + FOLDER1_NAME + "\";", "    stop;", "}", "", "# Folder 2", "if anyof (header :is \"subject\" \"" + SUBJECT2 + "\" )", "{", "    fileinto \"/" + FOLDER1_NAME + "/" + FOLDER2_NAME + "\";", "    stop;", "}", "", "# Folder 3", "if anyof (header :is \"subject\" \"" + SUBJECT3 + "\" )", "{", "    fileinto \"" + FOLDER1_NAME + "/" + FOLDER2_NAME + "/" + FOLDER3_NAME + "\";", "    stop;", "}", "", "# Folder 4", "if anyof (header :is \"subject\" \"" + SUBJECT4 + "\" )", "{", "    fileinto \"/" + FOLDER4_NAME + "\";", "    stop;", "}" });
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void main(String[] args)
/*     */     throws Exception
/*     */   {
/* 361 */     TestUtil.cliSetup();
/* 362 */     TestUtil.runTest(TestFolderFilterRules.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestFolderFilterRules.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */